//package in.ac.cfilt.analysis.mr;
//package org.apache.nutch.analysis.mr;
package demo.in.ac.iitb.cfilt.analysis.en;

/**
 * Copyright 2004 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;

import edu.mit.jwi.Dictionary;
import edu.mit.jwi.item.POS;
import edu.mit.jwi.item.Word;
import edu.mit.jwi.morph.WordnetStemmer;

/**
 * A filter that stems Marathi words. It supports a table of words that should
 * not be stemmed at all. The stemmer used can be changed at runtime after the
 * filter object is created (as long as it is a Stemmer).
 * 
 * @author Gerhard Schwarz
 * @version $Id: MarathiStemFilter.java,v 1.3 2008/01/05 15:17:43 iitb Exp $
 */
@SuppressWarnings("deprecation")
public final class EnglishStemFilter extends TokenFilter {
	/**
	 * The actual token in the input stream.
	 */

	private WordnetStemmer stemmer = null;
	private Set<String> exclusionSet = null;
	private TermAttribute termAtt = addAttribute(TermAttribute.class);
	public Dictionary dict = new Dictionary(new File("/data1/arjun/Lucene3.5/WordNet-3.0/dict"));

	public EnglishStemFilter(TokenStream in) {
		super(in);
		try {
			if(!dict.isOpen()) {
			dict.open();
			}
			stemmer = new WordnetStemmer(dict);
		} catch (Exception e) {
			e.printStackTrace();
		}
		termAtt = addAttribute(TermAttribute.class);
	}

	/**
	 * Builds a MarathiStemFilter that uses an exclusiontable.
	 */
	public EnglishStemFilter(TokenStream in, Set exclusionSet) {

		this(in);
		// System.out.println("Inside constr.");
		this.exclusionSet = exclusionSet;
	}

	/**
	 * Set a alternative/custom Stemmer for this filter.
	 */
	public void setStemmer(WordnetStemmer stemmer) {
		if (stemmer != null) {
			this.stemmer = stemmer;
		}
	}

	/**
	 * Set an alternative exclusion list for this filter.
	 */
	public void setExclusionSet(Set exclusionSet) {
		this.exclusionSet = exclusionSet;
	}

	@Override
	public boolean incrementToken() throws IOException {
		if (input.incrementToken()) {
			// System.out.println("Inside Stemmer");

			// long start = System.currentTimeMillis();
			// long start = System.nanoTime();
			if (stemmer == null)
				System.out.println("NULLLLLLLLLLL");
			String term = termAtt.term();
			String finalStem = term;
			try {
			List<String> st = stemmer.findStems(term, null);
			if(!st.isEmpty()) {
				Vector<String> vt = new Vector<String>(st);
				finalStem = vt.firstElement();
			}
			}catch(Exception e) {
				
			}
//			List<String> st = stemmer.findStems(term, POS.NOUN);
//			if(st.isEmpty()) {
//				st = stemmer.findStems(term, POS.ADJECTIVE);
//			}else if(st.isEmpty()) {
//				st = stemmer.findStems(term, POS.VERB);
//			}else if(st.isEmpty()) {
//				st = stemmer.findStems(term, POS.ADVERB);
//			}else if(st.isEmpty()) {
//				st = stemmer.findStems(term, null);
//			}
//			
//			
//			Iterator<String> it = st.iterator();
//			String finalStem= term;
//			while (it.hasNext()) {
//				String stem = it.next();
//				if ((stem != null) && (!stem.equalsIgnoreCase("")) && stem.length() < finalStem.length()) {
//					finalStem= stem;
//				}
//			}
//			finalStem = finalStem.trim().toLowerCase();
			if(!finalStem.isEmpty() || !finalStem.equalsIgnoreCase(""))
				termAtt.setTermBuffer(finalStem);
			
			return true;// TODO Auto-generated method stub
		} else {
			dict.close();
			return false;
		}
	}
}
